package com.example.stujava.module.xiangxue.reflect.generic_type;

import java.lang.reflect.Field;
import java.lang.reflect.GenericArrayType;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.List;
import java.util.Map;

public class ParameterizedTypeStu {
    private Map<String, Integer> map;
    private List<String>[] list;

    public static void main(String[] args) throws Exception {
        Class cla = ClassLoader.getSystemClassLoader().loadClass("com.example.stujava.module.xiangxue.reflect.generic_type.ParameterizedTypeStu");
        Field field = ParameterizedTypeStu.class.getDeclaredField("map");
        //属性名称
        System.out.println(field.getName());//map
        //属性类型+泛型类型
        System.out.println(field.getGenericType());//java.util.Map<java.lang.String, java.lang.Integer>
        //属性
        ParameterizedType genericType = (ParameterizedType) field.getGenericType();
        System.out.println(genericType.getRawType());//interface java.util.Map
        for (Type type : genericType.getActualTypeArguments()) {
            System.out.println(type);//class java.lang.String,class java.lang.Integer
        }

        //数组 GenericArrayType
        Class listField =  Class.forName("com.example.stujava.module.xiangxue.reflect.generic_type.ParameterizedTypeStu");
        GenericArrayType genericArrayType = (GenericArrayType) listField.getDeclaredField("list").getGenericType();
        System.out.println(genericArrayType.getGenericComponentType());//java.util.List<java.lang.String>
    }
}
